package com.onemore.task.remote;

import java.text.SimpleDateFormat;

import org.voovan.http.server.HttpResponse;
import org.voovan.tools.log.Logger;

import com.alibaba.fastjson.JSONObject;

public class RmtTaskBase {
    private final String charset = "UTF-8";
	public String message = "成功";
    public SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
	protected final static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	/**
	 * 向服务器返回结果
	 * @param response
	 * @param ret
	 * @param message
	 * @return
	 */
	public boolean putResult(HttpResponse response,int ret,String message) {
		JSONObject json = new JSONObject();
		json.put("flag", (ret==0||ret==200)?"success":"failure");
		json.put("code", ret);
		json.put("message", message);
		String str = json.toJSONString();
		response.header().put("Content-Rangle", "bytes " + 0 + "-" + (str.length()));
		response.header().put("Content-Type", "text/html;charset="+charset);
		response.body().write(str);
		return true;
	}
	public boolean isFieldEmpty(String field)
	{
		if(field == null || field == "" || field.length() <= 0) {
			return true;
    	} else {
    		return false;
    	}
	}
	public boolean checkOutRequired(JSONObject json,String strRequireds[]) throws IllegalArgumentException
	{
		for(String strRequired:strRequireds) {
			if(!json.containsKey(strRequired)) {
				//IllegalArgumentException e = new IllegalArgumentException("报文缺少'"+strRequired+"'字段");
				throw new IllegalArgumentException("报文缺少'"+strRequired+"'字段");
			}
		}
		return true;
	}
	public void setDefault(JSONObject json,String key,Object val)
	{
		if(!json.containsKey(key)) {
			json.put(key, val);
		}
	}
}
